package cn.hx.admin.controller;

import cn.hx.admin.entity.Dept;
import cn.hx.admin.service.DeptService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
/**
 * 部门表(Dept)表控制层
 *
 * @author makejava
 * @since 2023-12-19 14:48:03
 */
@RestController
@RequestMapping("dept")
@CrossOrigin
public class DeptController {
    /**
     * 服务对象
     */
    @Resource
    private DeptService deptService;

    /**
     * 分页查询
     *
     * @param dept 筛选条件
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<List<Dept>> queryByPage(Dept dept) {
        return ResponseEntity.ok(this.deptService.queryByPage(dept));
    }
    @GetMapping("tree")
    public ResponseEntity<List<Dept>> queryByPage() {
        return ResponseEntity.ok(this.deptService.tree());
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Dept> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.deptService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param dept 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Dept> add(Dept dept) {
        return ResponseEntity.ok(this.deptService.insert(dept));
    }

    /**
     * 编辑数据
     *
     * @param dept 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Dept> edit(@RequestBody Dept dept) {
        return ResponseEntity.ok(this.deptService.update(dept));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Long id) {
        return ResponseEntity.ok(this.deptService.deleteById(id));
    }

}

